﻿using AdverseEventReportSystem.BLL.SchedulingSystem;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Scheduling.Controllers
{
    public class SchedulingSummaryController : BaseController
    {
        // GET: Scheduling/SchedulingSummary
        public ActionResult Index()
        {
            ViewBag.StartDate = WebUtils.FirstDayOfMonth(DateTime.Now);
            ViewBag.EndDate = WebUtils.LastDayOfMonth(DateTime.Now);
            return View();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            string personnelId = WebUtils.GetString("personnelId");
            string personnelName = WebUtils.GetString("personnelName");
            int categoryId = WebUtils.GetInt("categoryId", -1);
            string startDate = WebUtils.GetString("startDate");
            string endDate = WebUtils.GetString("endDate");

            SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
            ResultHelperOfDataTable serviceResult = service.GetSchedulingSum(loginUser.CurrDepCode, personnelId, personnelName, categoryId, DataConvert.StrToDateTime(startDate, DateTime.Now), DataConvert.StrToDateTime(endDate, DateTime.Now));
            DataTable dt = new DataTable();
            if (serviceResult.S)
            {
                dt = serviceResult.D;
            }
            var result = new { Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 获取表头
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSchedulingSumTitle()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                string personnelId = WebUtils.GetString("personnelId");
                string personnelName = WebUtils.GetString("personnelName");
                int categoryId = WebUtils.GetInt("categoryId", -1);
                string startDate = WebUtils.GetString("startDate");
                string endDate = WebUtils.GetString("endDate");

                SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
                ResultHelperOfDataTable result = service.GetSchedulingSumTitle(loginUser.CurrDepCode, personnelId, personnelName, categoryId, DataConvert.StrToDateTime(startDate, DateTime.Now), DataConvert.StrToDateTime(endDate, DateTime.Now));
                if (result.S)
                {
                    resultInfo.Body = result.D;
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = result.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }

            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 获取班别列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetSchedulingCategoryByRecord()
        {
            string startDate = WebUtils.GetString("startDate");
            string endDate = WebUtils.GetString("endDate");
            DataTable dt = null;
            SchedulingSystemSoapClient service = new SchedulingSystemSoapClient();
            ResultHelperOfDataTable result = service.GetSchedulingCategoryByRecord(DataConvert.StrToDateTime(startDate, DateTime.Now), DataConvert.StrToDateTime(endDate, DateTime.Now), loginUser.CurrDepCode);
            if (result.S)
            {
                dt = result.D;
            }
            if (dt == null)
            {
                dt.Columns.Add("CategoryId");
                dt.Columns.Add("CategoryCode");
            }
            DataRow dr = dt.NewRow();
            dr["CategoryId"] = -1;
            dr["CategoryCode"] = "请选择";
            dt.Rows.InsertAt(dr, 0);

            return JsonDate(dt);
        }
    }
}